/*
 * Copyright 2011 Witoslaw Koczewsi <wi@koczewski.de>, Artjom Kochtchi
 * 
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero
 * General Public License as published by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package scrum.client.dashboard;

import java.util.Collections;
import java.util.List;

import scrum.client.common.AScrumWidget;
import scrum.client.project.Project;
import scrum.client.project.Scenario;

import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;

public class ScenariosWidget extends AScrumWidget {

	private HTML html;

	@Override
	protected Widget onInitialization() {
		html = new HTML();
		return html;
	}

	@Override
	protected void onUpdate() {
		Project project = getCurrentProject();
		StringBuilder sb = new StringBuilder();
		sb.append("<ul class='ScenariosWidget'>");

		List<Scenario> scenarios = project.getScenarios();

		Collections.sort(scenarios, Scenario.Comparators.CRITICISM);
		if (!scenarios.isEmpty()) {
			for (Scenario scenario : scenarios) {
				sb.append("<p>");
				if ((scenario.getStoriesAssociates() == null) || (scenario.getStoriesAssociates().size() == 0)) {
					sb.append("<img src=\"redButton.png\" height=\"10\" width=\"10\"/> ");
					sb.append("<span style='color: red;'>");
					sb.append(scenario.toHtml());
					sb.append("</span>");
					sb.append("<span style='color: gray;font-size: 7pt;font-weight: bold'>");
					sb.append(" (There is not requirements associated to Scenario)");
					sb.append("</span>");
				} else {
					if ((scenario.getPercentLabel() >= 0) && (scenario.getPercentLabel() < 30)) {
						sb.append("<img src=\"redButton.png\" height=\"10\" width=\"10\"/> ");
						sb.append("<span style='color: red;'>");
						sb.append(scenario.toHtml());
						sb.append("</span>");
						sb.append("<span style='color: gray;font-size: 7pt;font-weight: bold'>");
						sb.append(" (");
						sb.append(scenario.getTaskStatusLabel());
						sb.append(")");
						sb.append("</span>");
					} else {
						if ((scenario.getPercentLabel() >= 30) && (scenario.getPercentLabel() < 100)) {
							sb.append("<img src=\"orangeButton.png\" height=\"10\" width=\"10\"/> ");
							sb.append("<span style='color: #FFBF00;'>");
							sb.append(scenario.toHtml());
							sb.append("</span>");
							sb.append("<span style='color: gray;font-size: 7pt;font-weight: bold'>");
							sb.append(" (");
							sb.append(scenario.getTaskStatusLabel());
							sb.append(")");
							sb.append("</span>");
						} else {
							sb.append("<img src=\"greenButton.png\" height=\"10\" width=\"10\"/> ");
							sb.append("<span style='color: green;'>");
							sb.append(scenario.toHtml());
							sb.append("</span>");
							sb.append("<span style='color: gray;font-size: 7pt;font-weight: bold'>");
							sb.append(" (");
							sb.append(scenario.getTaskStatusLabel());
							sb.append(")");
							sb.append("</span>");
						}
					}

				}

				sb.append("</p>");
			}
		}

		sb.append("</ul>");
		html.setHTML(sb.toString());
	}
}
